<template>
  <div class="app-container">
    <!-- 表格 -->
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="顺序" type="index" align="center" width="50" />
      <el-table-column label="店铺名称" prop="shopping_name" align="center" />
      <el-table-column label="保证金" prop="security_deposit" align="center" />
      <el-table-column label="待拍商品数" prop="wait_pro_num" align="center" />
      <el-table-column
        label="拍卖中商品数"
        prop="auction_pro_num"
        align="center"
      />
      <el-table-column label="订单数" prop="order_num" align="center" />
      <el-table-column label="入驻时间" prop="created_at" align="center" />
      <el-table-column label="商户状态" align="center">
        <template slot-scope="{ row }">
          {{
            row.audit_status == 0
              ? "待审核"
              : row.audit_status == 1
                ? "审核通过"
                : row.audit_status == 2
                  ? "不通过"
                  : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="{ row }">
          <el-button
            size="mini"
            plain
            type="primary"
            @click="handleLook(row.id)"
          >
            查看
          </el-button>
          <el-button
            v-if="row.using_status == 1"
            size="mini"
            plain
            type="danger"
            @click="handleDisables(row.id)"
          >
            禁用
          </el-button>
          <el-button
            v-if="row.using_status == 0"
            size="mini"
            plain
            type="success"
            @click="handleUnDisables(row.id)"
          >
            启用
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div v-if="total > 0" style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.per_page"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { getShopping, updateShopping } from '@/api/common'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'MerchantsManages',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      type: 0,
      listQuery: {
        page: 1,
        per_page: 20,
        type: 1,
        audit_status: 1
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      getShopping(this.listQuery)
        .then((response) => {
          const { data, total } = response.data
          this.list = data
          this.total = total
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    // 启用
    handleUnDisables(id) {
      this.$confirm('您确认启用当前商户吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          updateShopping(id, { using_status: 1 }).then((res) => {
            this.$notify({
              title: '提示',
              message: '启用成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        })
        .catch(() => {})
    },
    // 禁用
    handleDisables(id) {
      this.$confirm(
        '您确认禁用当前商户吗？禁用后，商户拍品将全部为待拍商品，商户列表中不显示当前商户',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          updateShopping(id, { using_status: 0 }).then((res) => {
            this.$notify({
              title: '提示',
              message: '禁用成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        })
        .catch(() => {})
    },
    // 查看
    handleLook(id) {
      this.$router.push('/merchants/info/' + id)
    }
  }
}
</script>
